import { merge, mapValues } from "lodash-es";
import { mapStore } from "../utils/store";
import { $http } from "../boot/http";

export default merge(
  {
    namespaced: true,
    state: {
      assets: null
    },
    mutations: {},
    getters: {
      assetsTotal(state, getters) {
        let total = {};
        for (const asset of getters.assets) {
          for (const coin in asset.balance_price) {
            if (!total.hasOwnProperty(coin)) {
              total[coin] = 0;
            }
            total[coin] += asset.balance_price[coin];
          }
        }

        return total;
      }
    },
    actions: {
      async withdraw({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "asset.withdraw",
            loading: async () => {
              const { data } = await $http.post(
                "m/coin/api/v1/withdraw",
                params
              );
              return data;
            }
          },
          {
            root: true
          }
        );
      },
      async loadCoinAddress({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "asset.coinAddress",
            loading: async () => {
              const { data } = await $http.get(
                "m/coin/api/v1/wallets/" + params.chain
              );
              return data;
            }
          },
          {
            root: true
          }
        );
      },
      async internal({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "asset.internal",
            loading: async () => {
              const { data } = await $http.post(
                "m/coin/api/v1/internal_trade",
                params
              );
              return data;
            }
          },
          {
            root: true
          }
        );
      },
      async exchange({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "asset.exchange",
            loading: async () => {
              const { data } = await $http.post(
                "m/coin/api/v1/exchange/order",
                params
              );
              return data;
            }
          },
          {
            root: true
          }
        );
      },
      async exchangeSetting({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "asset.exchange_settings",
            loading: async () => {
              const { data } = await $http.get(
                "m/coin/api/v1/exchange/settings"
              );
              return data;
            }
          },
          {
            root: true
          }
        );
      }
    }
  },
  mapStore("assets", {
    url: "m/coin/api/v1/assets",
    getter(state) {
      const assets = state.assets || [];

      return assets
        .filter(asset => asset.coin)
        .map(asset => {
          return {
            ...asset,
            balance: parseFloat(asset.balance),
            balance_price: mapValues(asset.balance_price, price =>
              parseFloat(price)
            ),
            coin: {
              ...asset.coin,
              price: mapValues(asset.coin.price, price => parseFloat(price))
            }
          };
        });
    },
    options: {
      actionOptions: {
        commitType: "data"
      }
    },
    loadingKey: "asset.balances"
  }),
  mapStore("tranferLogs", {
    url: "m/coin/api/v1/assets/log_list",
    options: {
      actionOptions: {
        commitType: "infinite"
      }
    },
    loadingKey: "asset.tranfer_logs"
  }),
  mapStore("logModules", {
    url: "m/coin/api/v1/assets/log_module_list",
    options: {
      getterOptions: {
        defaultData: []
      },
      actionOptions: {
        commitType: "data"
      }
    },
    loadingKey: "asset.log_modules"
  })
);
